package com.globalblue;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class ConfigManager {
	public void readConfig() {
		Properties prop = new Properties();

		try {
			prop.load(new FileInputStream("config.properties"));

			String websiteDirectory = prop.getProperty("WEB_SITE_FOLDER");
			String[] websiteDirectoryList = websiteDirectory.split(",");
			Configurations.setWebsiteDirectoryList(websiteDirectoryList);
			String urlListString = prop.getProperty("URL_TO_LOAD");
			String[] urlList = urlListString.split(",");
			Configurations.setUrlToLoad(urlList);

		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

	public void writeConfig(String websiteFolder) {
		try {
			Properties prop = new Properties();
			prop.load(new FileInputStream("config.properties"));
			String currentValue = prop.getProperty("WEB_SITE_FOLDER");
			if (!currentValue.contains(websiteFolder)) {
				currentValue = currentValue.concat("," + websiteFolder);
				prop.setProperty("WEB_SITE_FOLDER", currentValue);
			}

			// save properties to project root folder
			prop.store(new FileOutputStream("config.properties"), null);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public static void addUrlToLoad(String url) {
		try {
			Properties prop = new Properties();
			prop.load(new FileInputStream("config.properties"));
			String currentValue = prop.getProperty("URL_TO_LOAD");
			if (!currentValue.contains(url)) {
				currentValue = currentValue.concat("," + url);
				prop.setProperty("URL_TO_LOAD", currentValue);

				// save properties to project root folder
				prop.store(new FileOutputStream("config.properties"), null);
			}
		} catch (Exception e) {

		}

	}
}
